package edu.tjpu.share.newui;

import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;
import edu.tjpu.share.R;

public class MainTabHost extends TabActivity {

	/**
	 * TabHost控件
	 */
	private TabHost mTabHost;

	/**
	 * TabWidget控件
	 */
	private TabWidget mTabWidget;

	private SharedPreferences share;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

		share = MainTabHost.this.getSharedPreferences("perference",
				MODE_PRIVATE);

		boolean isLogedIn = false;
		isLogedIn = share.getBoolean("isLogedIn", false);
		if (isLogedIn) {

			mTabHost = this.getTabHost();
			/* 去除标签下方的白线 */
			mTabHost.setPadding(mTabHost.getPaddingLeft(),
					mTabHost.getPaddingTop(), mTabHost.getPaddingRight(),
					mTabHost.getPaddingBottom() - 5);
			Resources rs = getResources();

			Intent layout1intent = new Intent();
			layout1intent.setClass(this, MessageList.class);
			TabHost.TabSpec layout1spec = mTabHost.newTabSpec("messagelist");
			layout1spec.setIndicator("下载",
					rs.getDrawable(android.R.drawable.stat_sys_download));
			layout1intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			layout1spec.setContent(layout1intent);
			mTabHost.addTab(layout1spec);

			Intent layout2intent = new Intent();
			layout2intent.setClass(this, NewMessage.class);
			TabHost.TabSpec layout2spec = mTabHost.newTabSpec("newmessage");
			layout2spec.setIndicator("上传",
					rs.getDrawable(android.R.drawable.stat_sys_upload));
			layout2intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			layout2spec.setContent(layout2intent);
			mTabHost.addTab(layout2spec);

			Intent layout3intent = new Intent();
			layout3intent.setClass(this, UserManage.class);
			TabHost.TabSpec layout3spec = mTabHost.newTabSpec("usermanage");
			layout3spec.setIndicator("其他",
					rs.getDrawable(android.R.drawable.stat_notify_more));
			layout3intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			layout3spec.setContent(layout3intent);
			mTabHost.addTab(layout3spec);

			/* 对Tab标签的定制 */
			mTabWidget = mTabHost.getTabWidget();
			for (int i = 0; i < mTabWidget.getChildCount(); i++) {
				/* 得到每个标签的视图 */
				View view = mTabWidget.getChildAt(i);
				/* 设置每个标签的背景 */
				if (mTabHost.getCurrentTab() == i) {
					view.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.number_bg_pressed));
				} else {
					view.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.number_bg));
				}
				/* 设置Tab间分割竖线的颜色 */
				// tabWidget.setBackgroundColor(Color.WHITE);
				/* 设置Tab间分割竖线的背景图片 */
				// tabWidget.setBackgroundResource(R.drawable.icon);
				/* 设置tab的高度 */
				//mTabWidget.getChildAt(i).getLayoutParams().height = 80;
				TextView tv = (TextView) mTabWidget.getChildAt(i).findViewById(
						android.R.id.title);
				/* 设置tab内字体的颜色 */
				//tv.setTextColor(Color.rgb(49, 116, 171));
			}

			/* 当点击Tab选项卡的时候，更改当前Tab标签的背景 */
			mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
				@Override
				public void onTabChanged(String tabId) {
					for (int i = 0; i < mTabWidget.getChildCount(); i++) {
						View view = mTabWidget.getChildAt(i);
						if (mTabHost.getCurrentTab() == i) {
							view.setBackgroundDrawable(getResources()
									.getDrawable(R.drawable.number_bg_pressed));
						} else {
							view.setBackgroundDrawable(getResources()
									.getDrawable(R.drawable.number_bg));
						}
					}
				}
			});
		} else {
			Toast.makeText(MainTabHost.this, "请先登录！", Toast.LENGTH_SHORT)
					.show();
			Intent intent = new Intent();
			intent.setClass(MainTabHost.this, Welcome.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
		}

	}
}
